package com.netease.easyexcel.util;

import org.springframework.util.StringUtils;

import java.util.UUID;
import java.util.regex.Pattern;

/**
 * @author xianyu
 * @version 1.0
 * @date 2021/10/3 17:36
 */
public class ColumnUtil {

    public static String entityName(String tableName) {
        return processFieldName(tableName, true);
    }

    public static String fieldName(String columnName) {
        return processFieldName(columnName, false);
    }

    private static String processFieldName(String fieldName, boolean isTable) {
        if (StringUtils.isEmpty(fieldName)) {
            return "UNKNOWN"+ UUID.randomUUID().toString().replaceAll("-", "");
        }
        Pattern letter = Pattern.compile("[A-Za-z]");
        Pattern characters = Pattern.compile("[A-Za-z0-9]");
        Pattern lowercase = Pattern.compile("[a-z]");
        int i = 0;
        while (i<fieldName.length()) {
            // 剔除前面的数字和特殊字符，保证字母开头（JDL语法限制）
            if (letter.matcher(fieldName.substring(i, i+1)).matches()) {
                break;
            }
            i++;
        }
        if (i==fieldName.length()) {
            return "UNKNOWN"+UUID.randomUUID().toString().replaceAll("-", "");
        }
        String col = fieldName.substring(i);
        StringBuilder sb = new StringBuilder();
        for (i = 0;i<col.length();i++) {
            String c = String.valueOf(col.charAt(i));
            if (characters.matcher(c).matches()) {
                sb.append(c);
            } else {
                // 遇到特殊字符，后续小写字符大写
                if (i + 1 < col.length()) {
                    String next = String.valueOf(col.charAt(i+1));
                    if (lowercase.matcher(next).matches()) {
                        sb.append(next.toUpperCase());
                        i++;
                    }
                }
            }
        }

        if (isTable) {
            // 表名转实体时，首字母必须大写（JDL语法限制）
            return StringUtils.capitalize(sb.toString());
        } else {
            return StringUtils.uncapitalize(sb.toString());
        }
    }

    public static String type(String type) {
        if (type.startsWith("varchar")) {
            return "String";
        } else if (type.startsWith("bigint")) {
            return "Long";
        } else if (type.contains("int")) {
            return "Integer";
        } else if (type.startsWith("float")) {
            return "Float";
        } else if (type.startsWith("double")) {
            return "Double";
        } else if (type.startsWith("bit(1)")) {
            return "Boolean";
        } else if (type.startsWith("bit")) {
            return "Integer";
        } else if (type.startsWith("decimal")) {
            return "BigDecimal";
        } else if (type.startsWith("datetime")) {
            return "Instant";
        } else if (type.startsWith("timestamp")) {
            return "ZonedDateTime";
        } else if (type.startsWith("date")) {
            return "LocalDate";
        } else if (type.contains("blob")) {
            return "AnyBlob";
        } else if (type.contains("text")) {
            return "String";
        }
        return "String";
    }

}
